<?php
namespace SimplePhp\System;
use SimplePhp\System\AutoLoad;
use SimplePhp\System\File;

/**
 * 系统默认载入以及初始化
 * 保存运行时的上下文
 */
class Context {
	private static $context;
	private static $config = [];
	private $root;
	private $controllerPath;
	/**
	 * 私有化构造函数
	 */
	private function __construct() {
		$this->root = CORE_PATH;
		$this->iniErrorHandler();
	}
	/**
	 * 单例获取
	 * @return [type] [description]
	 */
	public static function getInstanct() {
		if (empty(self::$context)) {
			self::$context = new Context();
		}
		return self::$context;
	}
	public function __get($key) {
		return $this->$key;
	}
	/**
	 * 设置Controller的文件夹
	 * 自动加载
	 * @param [type] $path [description]
	 */
	public function setControllerPath($path) {
		if (!is_dir($path)) {
			throw new \Exception("文件夹不存在", 1);
		}
		define("CONTROLLER_PATH", $path);
		$this->controllerPath = $path;
		//挂在自动载入的函数
		AutoLoad::registerAutoload('controller', function ($class) {
			$resolve = AutoLoad::autoloadCalssResolve($class);
			$class = $resolve[1] . '.' . substr($resolve[0], 1, -1) . '.php';
			$filePath = CONTROLLER_PATH . DIRECTORY_SEPARATOR . $class;
			if (is_file($filePath)) {
				require ($filePath);
			}
		});
	}
	/**
	 * 初始化载入函数
	 * @return [type] [description]
	 */
	public function iniAutoLoad() {
		if (empty($this->root)) {
			throw new Exception("还未设置项目根目录", 1);
		}
		require_once "AutoLode.class.php";
		require_once "File.class.php";
		$file = new File($this->root);
		AutoLoad::IniAutoLoad($file);
		$this->iniAutoConfig();
	}

	/**
	 * 初始化配置文件扫描
	 * @return [type] [description]
	 */
	public function iniAutoConfig() {
		$filelist = (new File($this->root))->autoloadFileMap;
		foreach ($filelist as $key => $value) {
			if (strpos($key, "config.php") !== false || strpos($key, "Config.php") !== false) {
				$config = require $value;
				self::$config = array_merge(self::$config, $config);
			}
		}
	}

	/**
	 * 获取配置
	 * @param  [type] $key [description]
	 * @return [type]      [description]
	 */
	public function getConfig($key = null) {
		if ($key != null) {
			return self::$config[$key];
		} else {
			return self::$config;
		}
	}

	/**
	 * 注册错误显示
	 * @return [type] [description]
	 */
	public function iniErrorHandler() {
		error_reporting(E_ERROR);
		set_error_handler(function ($errno, $errstr, $errfile, $errline, $errcontext) {
			// $context = var_export($errcontext); //上下文变量
			if ($errno == E_USER_ERROR) {
				$errno = "错误（error）";
			} else if ($errno == E_USER_WARNING) {
				$errno = "警告 （warning）";
			} else {
				$errno = "提醒（notice）";
			}
			$error = <<<STR
			<div style="border: 2px solid #000;margin:10px;overflow:scroll;word-wrap: break-word;">
				<h1>错误等级:$errno</h1>
				<div style="width:auto;height:30dx;overflow:hidden;word-wrap: break-word;">
					错误消息:$errstr
				</div>
				<div style="width:auto;height:30dx;overflow:hidden;word-wrap: break-word;">
					错误文件：$errfile
					  行号:$errline
				</div>
				<div style="width:500px;height:30dx;overflow:hidden;word-wrap: break-word;">
STR;
			echo $error;
			var_dump($errcontext);
			$error2 = <<<STR
				</div>
STR;

			echo $error2;
			echo "<div style='width:auto;height:30dx'>
						<pre>";
			debug_print_backtrace();
			echo "</pre>
				</div>
			</div>";
		}, E_ERROR);
		set_exception_handler(function ($exception) {
			var_dump($exception);
		});
	}

	/**
	 * 取得当前版本是否符合系统要求
	 * @return [type] [0:不符合要求  1:php版本大于5.50  2:php版本大于7.0.0]
	 */
	public function getPhpVersion() {
		if (version_compare(PHP_VERSION, '5.5.0') >= 0) {
			return 1;
		} else if (version_compare(PHP_VERSION, '7.0.0') >= 0) {
			return 2;
		}
		return 0;
	}
}